package com.lepu.app.fun.grow.adapter;

import java.io.InputStream;
import java.util.List;

import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.widget.CircularImageView;
import com.eyzhs.app.R;
import com.lepu.app.fun.grow.setting.GrowSettingAlbumImageActivity;

import android.annotation.SuppressLint;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;

@SuppressWarnings({ "deprecation", "unused" })
public class GrowSettingAlbumGalleryAdapter extends BaseAdapter {

	private GrowSettingAlbumImageActivity mActivity = null;
	private List<String> mDataList = null;

	private int mImageWidth = 0;

	public GrowSettingAlbumGalleryAdapter(
			GrowSettingAlbumImageActivity activity, List<String> list) {
		mActivity = activity;
		mDataList = list;

		int width = UtilityBase.getWindowWidthHeight(activity)[0];
		mImageWidth = width;
	}

	@Override
	public int getCount() {
		int count = 0;
		if (mDataList != null && mDataList.size() > 0)
			count = Integer.MAX_VALUE;

		return count;
	}

	@Override
	public Object getItem(int position) {
		return mDataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		int selectIndex = position % mDataList.size();

		if (convertView == null) {
			convertView = LayoutInflater.from(mActivity).inflate(
					R.layout.grow_setting_album_gallery_item, null);

			viewHolder = new ViewHolder();

			FrameLayout focusLayout = (FrameLayout) convertView
					.findViewById(R.id.focusLayout);
			ImageView galleryImageView = (ImageView) convertView
					.findViewById(R.id.galleryImageView);
			CircularImageView uploadIconImageView = (CircularImageView) convertView
					.findViewById(R.id.uploadIconImageView);
			RelativeLayout uploadHeadLayout = (RelativeLayout) convertView.findViewById(R.id.uploadHeadLayout);

			viewHolder.focusLayout = focusLayout;
			viewHolder.galleryImageView = galleryImageView;
			viewHolder.uploadIconImageView = uploadIconImageView;
			viewHolder.uploadHeadLayout = uploadHeadLayout;

			LayoutParams params = (LayoutParams) viewHolder.galleryImageView
					.getLayoutParams();
			params.width = mImageWidth / 2;
			//params.height = params.width * 9 / 16;
			viewHolder.galleryImageView.setLayoutParams(params);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		//String path = mDataList.get(selectIndex);

		viewHolder.uploadIconImageView.setTag(selectIndex);

		if (mActivity.mBitmapMap.size() > 0) {
			String imagePath = mActivity.mBitmapMap.get(selectIndex);
			if (!TextUtils.isEmpty(imagePath)) {
				viewHolder.uploadIconImageView.setImageBitmap(ImageUtilBase
						.getBitmap(imagePath));
			}
		}
		
		if(selectIndex == 0){
			viewHolder.uploadHeadLayout.setVisibility(View.GONE);
		}else{
			viewHolder.uploadHeadLayout.setVisibility(View.VISIBLE);
		}

		try {
//			AssetManager assetManager = mActivity.getAssets();
//			InputStream is = assetManager.open(path);
//			Bitmap bitmap = BitmapFactory.decodeStream(is);
			Bitmap firstBitmap = null;
			if (selectIndex == 0) {
				firstBitmap = ImageUtilBase.getBitmapByResId(mActivity
						,
						R.drawable.album_cover_default);
			}
			else if (selectIndex == 1) {
				firstBitmap = ImageUtilBase.getBitmapByResId(mActivity
						,
						R.drawable.album_cover0);
			} else if (selectIndex == 2) {
				firstBitmap = ImageUtilBase.getBitmapByResId(
						mActivity,
						R.drawable.album_cover1);
			} else if (selectIndex == 3) {
				firstBitmap = ImageUtilBase.getBitmapByResId(
						mActivity,
						R.drawable.album_cover2);
			}

			viewHolder.galleryImageView.setImageBitmap(firstBitmap);
		} catch (Exception e) {
		}
		
		viewHolder.focusLayout.setTag("cover_"+selectIndex);

		return convertView;
	}

	static class ViewHolder {
		FrameLayout focusLayout;
		ImageView galleryImageView;
		CircularImageView uploadIconImageView;
		RelativeLayout uploadHeadLayout;
	}

}
